package mercurius.fitness.client.rb.exercise;

import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import mercurius.fitness.client.util.MercuriusHelper;
import mercurius.fitness.client.util.AbstractResponseCommand;
import net.pleso.framework.client.dal.SelectParams;
import net.pleso.framework.client.util.DateUtil;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class ExerciseDataSet {
	public ExerciseDataSet() {
		
	}

	public void select(final SelectParams params, final AsyncCallback callback) {
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {
				   
			   }

			public Object getObject() {
				return params;
			}

			public void setObject(Object obj) {
				Vector props = (Vector) obj;
				Exercise[] exercises = new Exercise[props.size()];
				for(int i=0;i<props.size();i++) {
					HashMap exercise = (HashMap)props.get(i);
					exercises[i] = new Exercise(
							
							new Integer(Integer.parseInt((String) exercise.get("id"))),
							DateUtil.parseDate((String) exercise.get("date")),
							(String) exercise.get("duration"),
							new Float(Float.parseFloat((String) exercise.get("distance"))),
							(String) exercise.get("distance_unit"),
							new Integer(Integer.parseInt((String) exercise.get("avg_hr"))),
							new Integer(Integer.parseInt((String) exercise.get("max_hr"))),							
							new Float(Float.parseFloat((String) exercise.get("avg_speed"))),
							new Float(Float.parseFloat((String) exercise.get("max_speed"))),							
							(String) exercise.get("avg_pace"),
							(String) exercise.get("best_pace"),
							(String) exercise.get("speed_unit"),
							(String) exercise.get("z1_time"),
							(String) exercise.get("z2_time"),
							(String) exercise.get("z3_time"),
							(String) exercise.get("z4_time"),
							(String) exercise.get("z5_time"),
							(String) exercise.get("activity"),
							(String) exercise.get("notes"),
							(String) exercise.get("title"),
							(String) exercise.get("location"),
							new Integer(Integer.parseInt((String) exercise.get("public")))
							
					);
					if(exercise.get("gps").equals("1")) {
						exercises[i].setIsGPS(true);
					}
					
					if(MercuriusHelper.isNumber((String) exercise.get("points"))) {
						exercises[i].setPoints(Integer.parseInt((String) exercise.get("points")));
					}
					
					if(MercuriusHelper.isNumber((String) exercise.get("calories"))) {
						exercises[i].setCalories(Integer.parseInt((String) exercise.get("calories")));
					}
					
				}
				
				callback.onSuccess(exercises);
			}
			 };
			 
			MercuriusHelper.updateMercurius(new Exercise(),MercuriusHelper.MERCURIUS_SELECT,callback,pass);
	}
	
	public void selectCount(AsyncCallback callback) {
		MercuriusHelper.updateMercurius(new Exercise(),MercuriusHelper.MERCURIUS_SELECT_COUNT,callback);
	}
	
	public void add(final Exercise exercise, AsyncCallback callback) {
			AbstractResponseCommand pass = new AbstractResponseCommand() {
			   public void execute() {
				   exercise.setID(new Integer(results));
			   }

			public Object getObject() {
				return null;
			}

			public void setObject(Object obj) {
				
			}
			 };
			
			MercuriusHelper.updateMercurius(exercise,MercuriusHelper.MERCURIUS_INSERT,callback,pass);
	}
	
	public void update(final Exercise exercise, AsyncCallback callback) {
		MercuriusHelper.updateMercurius(exercise,MercuriusHelper.MERCURIUS_UPDATE,callback);
	}
	
	public void get(final Integer id, final AsyncCallback callback)
	{
		
		AbstractResponseCommand pass = new AbstractResponseCommand() {
			
			   public void execute() {
				   
			   }

			public Object getObject() {
				return id;
			}

			public void setObject(Object obj) {
				
				
				HashMap m = (HashMap) obj;
				String z =  (String) m.get("z1_time");
				Date d = DateUtil.parseTime(z);
				
				Exercise exercise = new Exercise(
						
						new Integer(Integer.parseInt((String) m.get("id"))),
						DateUtil.parseDate((String) m.get("date")),
						(String) m.get("duration"),
						new Float(Float.parseFloat((String) m.get("distance"))),
						(String) m.get("distance_unit"),
						new Integer(Integer.parseInt((String) m.get("avg_hr"))),
						new Integer(Integer.parseInt((String) m.get("max_hr"))),							
						new Float(Float.parseFloat((String) m.get("avg_speed"))),
						new Float(Float.parseFloat((String) m.get("max_speed"))),							
						(String) m.get("avg_pace"),
						(String) m.get("best_pace"),
						(String) m.get("speed_unit"),
						(String) m.get("z1_time"),
						(String) m.get("z2_time"),
						(String) m.get("z3_time"),
						(String) m.get("z4_time"),
						(String) m.get("z5_time"),
						(String) m.get("activity"),
						(String) m.get("notes"),
						(String) m.get("title"),
						(String) m.get("location"),
						new Integer(Integer.parseInt((String) m.get("public")))
						
				);
				
				if(m.get("gps").equals("1")) {
					exercise.setIsGPS(true);
				}
				
				if(MercuriusHelper.isNumber((String) m.get("points"))) {
					exercise.setPoints(Integer.parseInt((String) m.get("points")));
				}
				
				if(MercuriusHelper.isNumber((String) m.get("calories"))) {
					exercise.setCalories(Integer.parseInt((String) m.get("calories")));
				}

				callback.onSuccess(exercise);
			}
			 };
		
			 MercuriusHelper.updateMercurius(new Exercise(),MercuriusHelper.MERCURIUS_SELECT_BY_ID,callback,pass);

	}
	
	public void delete(Integer id, AsyncCallback callback)
	{
		Exercise exercise = new Exercise();
		exercise.setID(id);
		MercuriusHelper.updateMercurius(exercise,MercuriusHelper.MERCURIUS_DELETE,callback);
	}
}
